package com.touchportfolio.view
{
	import com.touchportfolio.loadQueue.LoadQueue;
	import com.touchportfolio.loadQueue.loaders.VideoLoader;
	import com.touchportfolio.model.ApplicationModel;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class Video extends Sprite
	{
		private var _appModel:ApplicationModel = ApplicationModel.getInstance();
		private var _videoLoader:VideoLoader;
		private var _buttonPlay:VideoPlayButton;
		private var _loading:LoadingView;
		private var _clicked:Boolean = false;
		private var _loaded:Boolean = false;
		
		public function Video(url:String,width:Number,height:Number)
		{
			_videoLoader = new VideoLoader(url,width,height);
			_videoLoader.addEventListener(VideoLoader.LOADED, videoLoaded);
			_videoLoader.addEventListener(VideoLoader.VIDEO_FINISHED, videoFinshed);
			
			_appModel.addEventListener(ApplicationModel.PLAY_VIDEO,playVideo);
			_appModel.addEventListener(ApplicationModel.STOP_VIDEO,stopVideo);
			
			//_q.addTask(_videoLoader);
			
			_buttonPlay = new VideoPlayButton();
			_buttonPlay.visible = true;
			_buttonPlay.x = width/2 - (_buttonPlay.width/2);
			_buttonPlay.y = height/2 - (_buttonPlay.height/2);
			
			_loading = new LoadingView();
			_loading.visible = false;
			_loading.x = width/2 - (_loading.width/2);
			_loading.y = height/2 - (_loading.height/2);
			
			var bg:Sprite = new Sprite();
			bg.graphics.beginFill(0x000000);
			bg.graphics.drawRect(0, 0, width, height);
			bg.graphics.endFill();
			this.addChild(bg);
			
			var clickArea:Sprite = new Sprite();
			clickArea.graphics.beginFill(0x00FF00, 0);
			clickArea.graphics.drawRect(0, 0, width, height);
			clickArea.graphics.endFill();
			
			this.addChild(_videoLoader);
			this.addChild(_buttonPlay);
			this.addChild(_loading);
			this.addChild(clickArea);
			
			clickArea.addEventListener(MouseEvent.CLICK, playModel);
		}
		
		private function playModel(e:Event):void{
			if(_appModel.playVideo)  _appModel.playVideo = false;
			else _appModel.playVideo = true;
		}
		
		private function playVideo(e:Event):void{
			if(_videoLoader)
			{
				if(_loaded == false){
					_loaded = true;
					_loading.visible = true;
					_buttonPlay.visible = false;
					_videoLoader.run();
				}else{
					_videoLoader.play();
					_buttonPlay.visible = false;
				}	
			}
		}
		
		public function stopVideo(e:Event = null):void{
			if(_videoLoader)
			{
				_videoLoader.pause();
				_buttonPlay.visible = true;	
			}
		}
		
		private function videoLoaded(e:Event):void{
			_loading.visible = false;
			_buttonPlay.visible = false;
			_loaded = true;
			_appModel.playVideo = true;
		}
		
		private function videoFinshed(e:Event):void{
			_buttonPlay.visible = true;
			_appModel.playVideo = false;
		}
		
		public function clearVideo():void
		{
			if(_videoLoader)
			{
				_appModel.playVideo = false;
				_videoLoader.stop();
				_videoLoader.seekSeconds(0);
				if(this.contains(_videoLoader)) this.removeChild(_videoLoader);
				_videoLoader = null;
			}
		}
	}
}